package org.lee.qotd;

import java.io.IOException;
import java.net.*;

/**
 * DatagramClient
 * 连接QOTD服务端
 *
 * @author dralee
 * @date 2021-12-28 18:38:13
 **/
public class DatagramClient {
    public static void main(String[] args) throws UnknownHostException {
        String domain = "djxmmx.net";
        int port = 17;
        InetAddress address = InetAddress.getByName(domain);
        try (DatagramSocket client = new DatagramSocket()) {
            DatagramPacket request = new DatagramPacket(new byte[1], 1, address, port);
            client.send(request);
            byte[] buffer = new byte[512];
            DatagramPacket response = new DatagramPacket(buffer, buffer.length);
            client.receive(response);
            String res = new String(buffer, 0, response.getLength());
            System.out.println(res);
        } catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
